﻿using AutoMapper;
using B.S.Base.ErrorCode;
using B.S.Base.Infrastructrue;
using B.S.Base.Moain.Entities;
using B.S.Base.Write.Api.Application.Command.Products;
using MediatR;

namespace B.S.Base.Write.Api.Application.CommandHander.Products
{
    public class UpdateProductCommandHander : IRequestHandler<UpdateProductCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Product> Pro;
        private readonly IMapper mapper;

        public UpdateProductCommandHander(IBaseRepository<Product> pro, IMapper mapper)
        {
            Pro = pro;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var getObj = Pro.GetAll().Result.FirstOrDefault(x => x.ProductId == request.ProductId);
            getObj.ProductGress = request.ProductGress;
            getObj.UpdatePeople = request.UpdatePeople;
            getObj.CreatePeople = request.CreatePeople;
            getObj.createTime = request.createTime;
            getObj.ProductEnterprise = request.ProductEnterprise;
            getObj.ProductNumber = request.ProductNumber;
            getObj.LotNumber = request.LotNumber;
            getObj.ShopId = request.ShopId;
            getObj.updateTime = DateTime.Now;
            //var model = mapper.Map<User>(request);
            var obj = Pro.Update(getObj).Result;
            if (obj > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = obj;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }

            return Task.FromResult(result);
        }
    }
}
